//
//  YBMsgPop.m
//  WaWaJiClient
//
//  Created by Rookie on 2017/11/21.
//  Copyright © 2017年 zego. All rights reserved.
//

#import "YBMsgPop.h"

@interface YBMsgPop()
{
    CGFloat stayTime;
    BOOL isClosekeyboard;
}
@property (nonatomic , assign)NSInteger backCountDown;
@property (nonatomic ,strong)NSTimer *backTimer;
@property (nonatomic , strong)UILabel *titleLab;
@end

@implementation YBMsgPop

+(void)showPop:(NSString *)msg {
    [SVProgressHUDTool dismiss];
    NSArray *array = [[UIApplication sharedApplication].keyWindow subviews];
    // ------for循环遍历
    for (YBMsgPop *alert in array) {
        
        // ------isKindOfClass 是否属于这个类
        if ([alert isKindOfClass:[YBMsgPop class]]) {
        
            // ------移除
            [alert removeFromSuperview];
        }
    }
    YBMsgPop *alert = [[YBMsgPop alloc]init];
    [alert setupView:ColorHex_Alpha(@"000000", 0.5) andStayTime:3 andTitleStr:msg  andTitleColor:[UIColor whiteColor] isClosekeyboard:NO];
    alert.center = CGPointMake(KScreen_W/2, KScreen_H*0.6);//[UIApplication sharedApplication].keyWindow.center;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
}
+(void)keyboardshowPop:(NSString *)msg {
    NSArray *array = [[UIApplication sharedApplication].keyWindow subviews];
    // ------for循环遍历
    for (YBMsgPop *alert in array) {
        
        // ------isKindOfClass 是否属于这个类
        if ([alert isKindOfClass:[YBMsgPop class]]) {
        
            // ------移除
//        [[UIApplication sharedApplication].keyWindow willRemoveSubview:alert];
            [alert removeFromSuperview];
        }
    }
    YBMsgPop *alert = [[YBMsgPop alloc]init];
    [alert setupView:ColorHex_Alpha(@"000000", 0.5) andStayTime:3 andTitleStr:msg  andTitleColor:[UIColor whiteColor] isClosekeyboard:YES];
    alert.center = CGPointMake(KScreen_W/2, KScreen_H*0.6);//[UIApplication sharedApplication].keyWindow.center;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
}

- (void)setupView:(UIColor *)color andStayTime:(CGFloat)setStayTime andTitleStr:(NSString *)titleStr andTitleColor:(UIColor *)titleColor isClosekeyboard:(BOOL) iskeyboard{
    isClosekeyboard = iskeyboard;
    stayTime = setStayTime;
    self.backgroundColor = color;//[UIFont systemFontOfSize:15]
    CGRect rrrrr = [self rectWithString:titleStr andFont:FontBold(15) andWidth:KScreen_W-80];
    CGFloat wwww;
    if (rrrrr.size.width > KScreen_W/3) {
        wwww = rrrrr.size.width + 20;
    }else {
        wwww = KScreen_W/3 + 20;
    }
    self.titleLab.frame = CGRectMake((wwww-rrrrr.size.width)/2, 10, rrrrr.size.width, rrrrr.size.height);
    self.frame = CGRectMake(0, 0, wwww, rrrrr.size.height+20);
    
    self.titleLab.textColor = titleColor;
    self.titleLab.text = titleStr;
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
    [self addGestureRecognizer:singleTap];
}
-(void)touch {
    [self setGoUpAnimation];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupViews];
        [self startPosition];
        [self setDeclineAnimation];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 8;
    UIButton * textShowButtonTo1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        NSString * textShowButtonTitleTo1 = @"测试功能";
        [textShowButtonTo1 setTitle:textShowButtonTitleTo1 forState:UIControlStateNormal];
        NSInteger textShowArcTo1 = arc4random_uniform(22);
        NSLog(@"随机数：%ld", (long)textShowArcTo1);
    }
    return self;
}
- (void)setupViews {
    self.titleLab = [[UILabel alloc]init];
    self.titleLab.font = FontBold(15);
    self.titleLab.numberOfLines = 0;
    [self addSubview:self.titleLab];
}
//初始位置
- (void)startPosition {
    self.transform = CGAffineTransformMakeScale(0.01, 0.01);
    if (isClosekeyboard) {
        self.center = CGPointMake(KScreen_W/2, KScreen_H*0.5+30);
    }else{
        self.center = CGPointMake(KScreen_W/2, KScreen_H*0.8);
    }
    //[UIApplication sharedApplication].keyWindow.center;
}
//最终位置
- (void)endPosition {
    self.transform = CGAffineTransformMakeScale(1, 1);
    if (isClosekeyboard) {
        self.center = CGPointMake(KScreen_W/2, KScreen_H*0.5+30);
    }else{
        self.center = CGPointMake(KScreen_W/2, KScreen_H*0.8);
    }//[UIApplication sharedApplication].keyWindow.center;
}
//下降
- (void)setDeclineAnimation {
    __block typeof(self) tmpSelf = self;
    __block typeof(stayTime) tmpstayTime = stayTime;
    [UIView animateWithDuration:0.15 animations:^{
        [tmpSelf endPosition];
    } completion:^(BOOL finished) {
        if (tmpstayTime >= 0) {
            [tmpSelf backTime];
        }
    }];
}
//上升
- (void)setGoUpAnimation {
    [self.backTimer invalidate];
    __block typeof(self) tmpSelf = self;
    [UIView animateWithDuration:0.3 animations:^{
        [tmpSelf startPosition];
    } completion:^(BOOL finished) {
        [tmpSelf removeSelf];
    }];
}
- (void)backTime {
    //设置倒计时总时长
    [self.backTimer invalidate];
    self.backCountDown = stayTime;
    self.backTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(viewBackNSTimer) userInfo:nil repeats:YES];
}
//使用NSTimer实现倒计时
- (void)viewBackNSTimer {
    self.backCountDown -- ;
    if (self.backCountDown == 0  || self.backCountDown < 0) {
        [self.backTimer invalidate];
        [self setGoUpAnimation];
    }
}
-(void)removeSelf {
    [self removeFromSuperview];
}

-(CGRect)rectWithString:(NSString *)string andFont:(UIFont *)font andWidth:(CGFloat)width {
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
    return rect;
}
@end
